/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


package io.netopen.hotbitmapgg.slice;

import io.netopen.hotbitmapgg.ResourceTable;
import io.netopen.hotbitmapgg.view.NewCreditSesameView;
import io.netopen.hotbitmapgg.view.OldCreditSesameView;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.animation.Animator;
import ohos.agp.animation.AnimatorValue;
import ohos.agp.components.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class MainAbilitySlice extends AbilitySlice {
    private PageSlider pageSlider;
    private List<Component> pageviews=new ArrayList<>();
    private Random random = new Random();

    private NewCreditSesameView newCreditSesameView;
    private OldCreditSesameView oldCreditSesameView;
    private Image mButton,mButton1;
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        pageSlider = (PageSlider) findComponentById(ResourceTable.Id_pager_slider);
        LayoutScatter layoutScatter = LayoutScatter.getInstance(getContext());
        DependentLayout dependentLayout1 = (DependentLayout) layoutScatter.parse(ResourceTable.Layout_pageSlider1, null, false);
        DependentLayout dependentLayout2 = (DependentLayout) layoutScatter.parse(ResourceTable.Layout_pageSlider2, null, false);
        pageviews.add(dependentLayout1);
        pageviews.add(dependentLayout2);

        pageSlider.setProvider(new PageSliderProvider() {
            @Override
            //获取当前窗体界面数
            public int getCount() {
                return pageviews.size();
            }

            //返回一个对象，这个对象表明了PagerAdapter适配器选择哪个对象放在当前的pageviews中
            @Override
            public Object createPageInContainer(ComponentContainer componentContainer, int i) {
                componentContainer.addComponent(pageviews.get(i));
                return pageviews.get(i);
            }

            //是从ViewGroup中移出当前View
            @Override
            public void destroyPageFromContainer(ComponentContainer componentContainer, int i, Object o) {
                (componentContainer).removeComponent(pageviews.get(i));
            }

            //断是否由对象生成界面
            @Override
            public boolean isPageMatchToObject(Component component, Object o) {

                return component == o;
            }
        });

        newCreditSesameView=(NewCreditSesameView)dependentLayout1.findComponentById(ResourceTable.Id_sesame_view);
        mButton=(Image)dependentLayout1.findComponentById(ResourceTable.Id_btn);
        mButton.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                int i = random.nextInt(950);
                newCreditSesameView.setSesameValues(i);
            }
        });

        oldCreditSesameView=(OldCreditSesameView)dependentLayout2.findComponentById(ResourceTable.Id_sesame_view);
        mButton1=(Image)dependentLayout2.findComponentById(ResourceTable.Id_btn);
        mButton1.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                int i = random.nextInt(950);
                oldCreditSesameView.setSesameValues(i);
            }
        });
    }


    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
